<template>
	<view>
		<input type="text" v-model="location" @input="searchGD" style="height: 50upx;" />
		<map id="map" ref="map" style="width: 100%; height: calc(100vh - 50upx - 44px);" :latitude="latitude"
			:longitude="longitude" :markers="covers" @tap="getMap">
		</map>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				location: '',
				latitude: 30.225590283822925,
				longitude: 120.4048882560432,
				covers: [{
					id:0,
					latitude: 30.225590283822925,
					longitude: 120.4048882560432,
					iconPath: '../../../../../static/aidex/images/location.png',
					width:40,
					height:20
				}],
				_mapContext: null
			}
		},
		onLoad(params) {
			console.log(params);
			const locations = params.locations.split(',')
			this.covers[0].longitude = this.longitude = locations[0]
			this.covers[0].latitude = this.latitude = locations[1]
		},
		onReady() {
			let that = this
			this._mapContext = uni.createMapContext("map", this).$getAppMap();
			this._mapContext.onclick = function(point) {
				console.log(point);
				const {
					longitude,
					latitude
				} = point
				that.$u.api.GaodeApi.coordinateConvert({
					key: '9670df2781265a076d02bd0aaf33dc1a',
					locations: longitude + ',' + latitude,
				}).then(res => {
					console.log(res);
					const locations = res.locations.split(',')
					console.log(locations);
					// that.covers[0].longitude = locations[0]
					// that.covers[0].latitude =  locations[1]
					that.covers = [{
						id:0,
						longitude: locations[0],
						latitude: locations[1],
						iconPath: '../../../../../static/aidex/images/location.png'
					}]
					if (res.status == 1) {
						that.$u.api.GaodeApi.inverseGeocode({
							key: '9670df2781265a076d02bd0aaf33dc1a',
							location: res.locations,
						}).then(geocode => {
							console.log('geocode:', geocode);
							that.location = geocode.regeocode.formatted_address
						})
					}
				}).catch(err => {
					console.log(err);
				})
			}
		},
		methods: {
			getMap(e) {
				console.log(e);
				console.log(this._mapContext);
			},
			searchGD(e) {
				console.log(e);
				this.$u.api.GaodeApi.searchKeyWord({
					key: '9670df2781265a076d02bd0aaf33dc1a',
					keywords: e.detail.value,
				}).then(res => {
					console.log(res);
				})
			},
		}
	}
</script>

<style>

</style>
